﻿	    var treeID = null;
	    function tree(treeID,treeNodes,funName){
		var fid = 0;
		var treeType = 0;
		var layer = 0;
		var parentidlist = "";
		var title = "";
		var lastDiv = null;

		this.id = treeID;
		this.drag = false;
		this.dragObj = null;
		this.oldClass = "";
		this.fid = 0;
		this.treeType = 0;
		this.layer = 0;
		this.parentidlist = "";
		this.title = ""
		this.lastDiv = null
		
		this.getPos=function(el,sProp){
			var iPos = 0;
			while (el!=null) {
				iPos+=el["offset" + sProp];
				el = el.offsetParent;
			}
			return iPos;
		}
		
		this.getETarget = function(e){
			if (!e){return null;}
			if (!e.srcElement && !e.target){return null;}
			obj = e.srcElement ? e.srcElement : e.target;
			if (obj == null){return null};
			
			while (obj.getAttribute("treetype") == null && obj.tagName != "BODY" && obj.tagName != "HTML"){
				obj = obj.parentNode;
				if(obj == null){break;}
			}
			return obj;
		}		
		
		//鼠标按下
		this.onmousedown = function(e){
		
			if (!e){return false;}
			
			if (this.dragObj){
				this.dragObj.className = "noselected";
			}
			
			this.dragObj = this.getETarget(e);
			if (!this.dragObj){return;}
			
			if (!this.dragObj.getAttribute("treetype")){return;}
			
			var mX = e.x ? e.x : e.pageX;
			var mY = e.y ? e.y : e.pageY;
			
			this.drag = true;
			this.dragObj.className = "treenode_selected";
			this.oldClass = "treenode_selected";
			
			var textContent = this.dragObj.textContent ? this.dragObj.textContent : this.dragObj.innerText; 
			textContent = textContent.replace("添加","");
			textContent = textContent.replace("编辑","");
			textContent = textContent.replace("删除","");
			//textContent = textContent.replace("移动","");
			

			this.ShowMove(mX,mY,textContent );
			
			treeID = this;
			document.onmousedown = function(){return false};
			document.onmousemove = function(e){
				if (tree==null){
					return;
				}
				treeID.document_onmousemove(e);
			}
			
		    alignElWithMouse(mX,mY);
		
		};
		
		this.onmouseup = function(e){this.document_onmouseup(e)};
		this.onmouseover = function(e){
			if (this.drag){
				this.oldClass = this.getETarget(e).className;
				if (this.getETarget(e).getAttribute("treeType")==1){
					this.getETarget(e).className = "treenode_over";
				}
				else{
					this.getETarget(e).className = "treenode_0_over";				
				}
			}
			var mX = e.x ? e.x : e.pageX;
			var mY = e.y ? e.y : e.pageY;
			alignElWithMouse(mX,mY)
			
		};
		this.onmouseout = function(e){
			if (this.drag){
				this.getETarget(e).className = this.oldClass;
				if (this.dragObj.id == this.getETarget(e).id){
					this.findObj(this.id + "_treenode_move").style.display="block";
				}
			}
			
		};
		this.init = function(){
			document.write("\n<div id=\"" + this.id + "_control\" class=\"contral\">");
			document.write("\n<div id = \"" + this.id + "_treenode_move\" class=\"treenode_move\"></div>")
			document.write("\n<div id = \"" + this.id + "_line\"></div>")
			document.write("\n<div class=\"treenode_0\" style=\"display:none;\" id = \"" + this.id + "_treenode_0\" onmouseup=\"" + this.id + ".onmouseup(event);\" onmouseover=\"" + this.id + ".onmouseover(event)\" onmouseout=\"" + this.id + ".onmouseout(event)\"></div>")
			document.write("\n<div class=\"treenode_1\" style=\"display:none;\" id = \"" + this.id + "_treenode\" onmousedown=\"" + this.id + ".onmousedown(event);\" onmouseup=\"" + this.id + ".onmouseup(event);\" onmouseover=\"" + this.id + ".onmouseover(event)\" onmouseout=\"" + this.id + ".onmouseout(event)\"></div>");
			for(i=0;i<treeNodes.length;i++){
				var nodeStr = "";
				for(treeNode in treeNodes[i]){
					nodeStr += treeNode + "=\"" + escape(treeNodes[i][treeNode]) + "\" ";
				}
				document.write("\n<div class=\"treenode_0_noselected\" id = \"" + this.id + "_treenode" + i + "_0\" index=\"" + i + "\" treetype=\"0\" ");
				document.write(nodeStr);
				document.write("onmouseup=\"" + this.id + ".onmouseup(event);\" onmouseover=\"" + this.id + ".onmouseover(event)\" onmouseout=\"" + this.id + ".onmouseout(event)\">");
				if (i>0){
					document.write(treeNodes[i].linetitle);
				}
				document.write("</div>");
				document.write("\n<div class=\"treenode_noselected\" id = \"" + this.id + "_treenode" + i + "\" index=\"" + i + "\" treetype=\"1\" ");
				document.write(nodeStr);
				if(treeNodes[i].layer =='0')
				{
				     document.write("onmousedown=\"" + this.id + ".onmousedown(event);\" onmouseup=\"" + this.id + ".onmouseup(event);\" onmouseover=\"" + this.id + ".onmouseover(event)\" onmouseout=\"" + this.id + ".onmouseout(event)\">" + treeNodes[i].subject +  " &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src=../images/cal_nextMonth.gif> &nbsp;<a href=\"#\" class=\"TopicButton\"  type=\"button\" onclick=\"javascript:window.location.href='classAdd.aspx?cid="+treeNodes[i].fid+"';\"><img src=\"../images/add.gif\"/>添加</a><a href=\"#\" class=\"TopicButton\" type=\"button\" onclick=\"javascript:window.location.href='" +  "classAdd"  + ".aspx?id="+treeNodes[i].fid+"';\"><img src=\"../images/submit.gif\"/>编辑</a><a href=\"#\" class=\"TopicButton\" type=\"button\" onclick=\"javascript:if(confirm('您要删除该项吗?')){success.style.display = 'block';HideOverSels('success');window.location.href='classDel.aspx?id=" + treeNodes[i].fid +  "';}\"><img src=\"../images/del.gif\" />删除</a></div>");
				}
				else
				{
				     document.write("onmousedown=\"" + this.id + ".onmousedown(event);\" onmouseup=\"" + this.id + ".onmouseup(event);\" onmouseover=\"" + this.id + ".onmouseover(event)\" onmouseout=\"" + this.id + ".onmouseout(event)\">" + treeNodes[i].subject +  " &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src=../images/cal_nextMonth.gif>&nbsp;<a href=\"#\" class=\"TopicButton\" type=\"button\" onclick=\"javascript:window.location.href='classAdd.aspx?cid="+treeNodes[i].fid+"';\"><img src=\"../images/add.gif\"/>添加</a><a href=\"#\" class=\"TopicButton\" type=\"button\" onclick=\"javascript:window.location.href='" +  "classAdd"  + ".aspx?id="+treeNodes[i].fid+"';\"><img src=\"../images/submit.gif\"/>编辑</a><a href=\"#\" class=\"TopicButton\" type=\"button\" onclick=\"javascript:if(confirm('您要删除该项吗?')){success.style.display = 'block';HideOverSels('success');window.location.href='classDel.aspx?id="+treeNodes[i].fid +  "';}\"><img src=\"../images/del.gif\" />删除</a></div>");
				}
			}			
		}
		
		document.write("\n</div>");
			
		
		this.findObj = function(objname){
			if (document.getElementById(objname)){
				return document.getElementById(objname);
			}
			else if(document.getElementsByName(objname)){
				return document.getElementsByName(objname)
			}
			else{
				return null;
			}
		}
		
		this.HideMove = function(){
			if (this.findObj(this.id + "_treenode_move").style.display!="none"){
				this.findObj(this.id + "_treenode_move").style.display="none";
			}
		}
		
		this.ShowMove = function(mX ,mY,innerHTML){
			this.findObj(this.id + "_treenode_move").innerHTML = innerHTML;
			this.findObj(this.id + "_treenode_move").style.left =(mX + 10) + "px";
			this.findObj(this.id + "_treenode_move").style.top = (mY) + "px";
		}
		
		this.SetMove = function(e,mX,mY){
			this.findObj(this.id + "_treenode_move").style.left =(mX + 10) + "px";
			this.findObj(this.id + "_treenode_move").style.top = (mY) + "px";			
		}
		
		
		//document事件
		this.document_onmouseup = function(e){
			if (this.drag){
				this.drag = false;
				try{
					this.getETarget(e).className = this.oldClass;
					
					index = this.getETarget(e).getAttribute("index");
					eval(funName+ "(treeNodes[index],treeNodes[this.dragObj.getAttribute(\"index\")],this.getETarget(e).getAttribute(\"treetype\"))");
				}
				catch(e){}
				
				this.HideMove();
				treeID = null;
				
				document.onmousedown = function(){return true};
				document.onmousemove = function(){null;};
			}

		}
		document.onmouseup = new Function(this.id + ".document_onmouseup();");
		
		this.document_onmousemove = function(e){
			e= e ? e : window.event;
			var mX = e.x ? e.x : e.pageX;
			var mY = e.y ? e.y : e.pageY;
			this.SetMove(e,mX,mY);
			
		}
		
		this.document_onselectstart = function(){
			return !this.drag;
		}
		document.onselectstart = new Function("return " + this.id + ".document_onselectstart ();");
		
		this.init();
			
	}	

	    function reSetTree(objN, objO, treetype)
	    {
	        if (objN.fid == objO.fid)
	           return;
	        var message = '您是否要将类别\"' + objO.name + '\"移动到类别\"' + objN.name + '\"';

	        if (treetype == 1)
	        {
	            message += '子版下吗 ?';
	        }
	        else
	        {
	            message += '之前吗 ?';
	        }

	        if (confirm(message))
	        {

	            var objnparentidlist = ',' + objN.parentidlist + ',';
	            if (objnparentidlist.indexOf(',' + objO.fid + ',') < 0)
	            {
	                success.style.display = 'block';
	                HideOverSels('success');
	                window.location.href = 'class.aspx?currentid=' + objO.fid + '&targetid=' + objN.fid + '&isaschildnode=' + treetype;
	            }
	            else
	            {
	                alert('不能将当前分类或类别作为其子类别的类别');
	            }
	        }
	    }
   
   
           function alignElWithMouse(x,y)
        {
        }
        
        
//隐藏指定元素位置下方的"下拉列表框",该函数主题解决ie6以前(包括ie6)的下拉列表框遮挡显示层的问题
function HideOverSels(objID)
{
    var sels = document.getElementsByTagName('select'); 
  
    for (var i = 0; i < sels.length; i++) 
    {
         if (Obj1OverObj2(document.getElementById(objID), sels[i]))
         {
            
            sels[i].style.visibility = 'hidden';  
         }
         else
         {
            sels[i].style.visibility = 'visible';
         }
    }
}

function getLeftPosition(Obj) 
{
    try
    {
        for (var sumLeft=0;Obj!=document.body;sumLeft+=Obj.offsetLeft,Obj=Obj.offsetParent);
        return sumLeft;
    }
    catch(e)
    {}
}

function getTopPosition(Obj) 
{
    try
    {
        for (var sumTop=0;Obj!=document.body;sumTop+=Obj.offsetTop,Obj=Obj.offsetParent);
        return sumTop;
    }
    catch(e)
    {}
}

//判断obj1是否遮挡了obj2
function Obj1OverObj2(obj1, obj2)
{ 
  var result = true; 
  
  var obj1Left = getLeftPosition(obj1) - document.body.scrollLeft; 
  var obj1Top = getTopPosition(obj1)  - document.body.scrollTop; 
  var obj1Right = obj1Left + obj1.offsetWidth; 
  var obj1Bottom = obj1Top + obj1.offsetHeight;
  var obj2Left = getLeftPosition(obj2) - document.body.scrollLeft; 
  var obj2Top = getTopPosition(obj2) - document.body.scrollTop; 
  var obj2Right = obj2Left + obj2.offsetWidth; 
  var obj2Bottom = obj2Top + obj2.offsetHeight;
 

  if (obj1Right <= obj2Left || obj1Bottom <= obj2Top || obj1Left >= obj2Right || obj1Top >= obj2Bottom) 
  {
     result = false; 
  }
    
  return result; 
}